define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Auction_agent, $window, $timeout, $ionicSlideBoxDelegate, GoPage, GoDialog, GoAddress) {

        $scope.isLoad = false; //可以加载
        //画面加载
        $scope.$on('$ionicView.afterEnter', function() {
            //动态设置高度
            var all = $window.innerHeight;
            //计算滚动条
            $scope.myObj = {
                "height": all - 44 + "px"
            };
            $scope.init_data(); //必须在定义之后
        }, false);

        //导航切换
        $scope.selectIndex = 0;
        $scope.slideChanged = function(index) {
            $scope.selectIndex = index;
        };
        $scope.activeSlide = function(index) {
            $scope.selectIndex = index;
            $ionicSlideBoxDelegate.$getByHandle('main-handle').slide(index);
        };

        /*//下拉刷新 判断哪个模块进行刷新
        $scope.doRefresh = function() {
            $timeout(function() {
                $scope.$broadcast('scroll.refreshComplete');
            }, 1000);
        };
        //加载更多
        $scope.doRefreshMore = function() {
            $timeout(function() {
                $scope.$broadcast('scroll.infiniteScrollComplete');
            }, 1000);
        };*/

        //数据模型
        /*$scope.auctionModel = new Object();
        $scope.auctionModel.region = "";
        $scope.auctionModel.is_all = true;
        $scope.auctionModel.store = new Array();*/

        //循环改变选项状态
        /*$scope.checkFor = function(status) {
            for(var i = 0; i < $scope.products.store.length; i++) {
                $scope.auctionModel.store[i] = status;
            }
        }

        //全选、反选
        $scope.checkAll = function() {
                if(!$scope.auctionModel.is_all) {
                    $scope.checkFor(false);
                } else {
                    $scope.checkFor(true);
                }
                //$scope.auctionModel.is_all=!$scope.auctionModel.is_all;
            }
            //单击选项
        $scope.checkDan = function() {
            for(var i = 0; i < $scope.products.store.length; i++) {
                if(!$scope.auctionModel.store[i]) {
                    $scope.auctionModel.is_all = false;
                    break;
                }
                $scope.auctionModel.is_all = true;
            }
        }*/

        //数据模型
        /*$scope.input = new Object();
        $scope.input.act_id = "";
        $scope.input.region = new Array();
        $scope.input.region.region_id = "";
        $scope.input.region.id_all = true;
        $scope.input.region.store = new Array();
        $scope.input.region.store.store_id = "";
        $scope.input.goods = new Object();
        $scope.input.goods.good_id = "";
        $scope.input.goods.margin_price = "";
        $scope.input.goods.start_price = "";
        $scope.input.goods.add_price = "";
        $scope.input.goods.floor_price = "";*/



        //加载数据
        $scope.init_data = function() {
            Auction_agent.activitySetting('act_id', function(data) {
                $scope.activity = data;
            });
            Auction_agent.acutionProduct('act_id', function(data) {
                $scope.products = data;
                // $scope.checkFor(true);
            });
            /*Auction_agent.auction_dealer('act_id',function(data){
                     $scope.region=data.region;
            });*/

            //显示区域
            Auction_agent.auction_dealer('act_id', function(data) {
                $scope.dealer = data;
                $scope.isable = $scope.dealer.change_price;
                //$scope.init_detailContent($scope.dealer.goods);
                //console.log($scope.dealer);
                $scope.$watch('scope.choose', function(newValue, oldValue) {
                    //console.log($scope.dealer);
                    for(var i = 0; i < $scope.dealer.region.length; i++) {
                        // console.log($scope.dealer.region[i].region_name);
                        //console.log(newValue);
                        if($scope.dealer.region[i].region_name == newValue) {
                            //console.log(i);
                            //console.log($scope.dealer.region[0].store);
                            $scope.store = $scope.dealer.region[i].store;
                            //console.log($scope.store);
                            $scope.select_all($scope.dealer.region[i].is_all);

                        }
                    }
                })
            });
        }
        

       /* //区域
        $scope.input = new Object();
        $scope.input.keywords = "";
        $scope.input.province = ""; //省市区三级联动
        $scope.input.city = "";
        $scope.input.district = "";

        //下拉侦听，目前只能省市区三级联动
        $scope.listen_province = function(data) {
            $scope.input.city = "";
            $scope.input.district = "";
        };
        $scope.listen_city = function(data) {
            $scope.input.district = "";
        };

        //定义绑定
        var items = new Array();
        var obj1 = {
            name: 'input.province'
        };
        var obj2 = {
            name: 'input.city'
        };
        var obj3 = {
            name: 'input.district'
        };
        items.push(obj1);
        items.push(obj2);
        items.push(obj3);
        GoAddress.init_load($scope, items);*/

        //auctionModel.region

        //显示经销商用
        /*$scope.store=new Array();
        $scope.seel=function(){
            for(var k in $scope.region){
                if($scope.auctionModel.region==$scope.region[k].region_id){
                  $scope.store= $scope.region[k].store;
                  break;
                }else{
                    $scope.store="";
                }
            }
        }*/

        //**********************************************************************************
        //$scope.id = $stateParams.id;
        $scope.scope = new Object();
        var goods_rule = new Array();
        $scope.goods_rule = new Array();
        $scope.choose_store = new Array();
        $scope.is_all = false;
        var goods = new Array();
        var region = new Array();
        $scope.show_store = new Array();
        $scope.show_region = new Array();
        $scope.region = new Array();
        $scope.scope.choose = '';
        	$scope.scope.choose_select = { region_id: 0, region_name: "请选择区域" };
        $scope.store = new Array();
        	$scope.doSelectRegion = function () {
			var list = new Array();
			$scope.dealer.region.forEach(obj => {
				if (obj.id == undefined)
					obj.id = obj.region_id;
				if (obj.name == undefined)
					obj.name = obj.region_name;
			});
			//id是值 name是显示
			GoDialog.showSelect("区域", $scope.dealer.region, $scope.scope.choose_select, function (data) {
				$scope.scope.choose = data.region_name;
				$scope.scope.choose_select = data;
			});
		}
        $scope.get_region = function() {
            $scope.get_store();
            if($scope.scope.choose) {
                if($scope.choose_store.length > 0) {
                    for(var i = 0; i < $scope.dealer.region.length; i++) {
                        if($scope.dealer.region[i].region_name == $scope.scope.choose) {
                            for(var j = 0; j < $scope.region.length; j++) {
                                if($scope.dealer.region[i].region_id == $scope.region[j].region_id) {
                                    $scope.region[i] = {
                                        "region_id": $scope.dealer.region[i].region_id,
                                        "is_all": $scope.scope.is_all,
                                        "store": $scope.choose_store
                                    }
                                    $scope.show_region[i] = {
                                        "region_name": $scope.dealer.region[i].region_name,
                                        "store": $scope.show_store
                                    }
                                    $scope.choose_store = new Array();
                                    $scope.show_store = new Array();
                                    return;
                                }

                            }
                            var n = $scope.region.length;
                            $scope.region[n] = {
                                "region_id": $scope.dealer.region[i].region_id,
                                "is_all": $scope.scope.is_all,
                                "store": $scope.choose_store
                            };
                            $scope.show_region[n] = {
                                "region_name": $scope.dealer.region[i].region_name,
                                "store": $scope.show_store
                            }
                            $scope.choose_store = new Array();
                            $scope.show_store = new Array();

                        }
                    }
                } else
                    GoDialog.toast("请选择代理商");
            } else
                GoDialog.toast("请选择地域");
            $scope.choose_store = new Array();
            region = $scope.region;
            //console.log($scope.region);

        }

        $scope.get_third = function() {
            //$scope.get_region();
            $scope.submit();
            // console.log(region);
        }
        $scope.get_store = function() {
            for(var i = 0; i < $scope.store.length; i++) {
                if($scope.store[i].checked == true) {
                    var n = $scope.choose_store.length;
                    $scope.choose_store[n] = {
                        "store_id": $scope.store[i].store_id
                    };
                    $scope.show_store[n] = {
                        "store_name": $scope.store[i].store_name
                    }
                }

            }
            //          console.log($scope.choose_store);
        }

        $scope.select_all = function(all) {
            if(all == true) {
                $scope.scope.is_all = true;
                for(var i = 0; i < $scope.store.length; i++) {
                    $scope.store[i].checked = true;
                }
            } else {
                $scope.scope.is_all = false;
                for(var i = 0; i < $scope.store.length; i++) {
                    $scope.store[i].checked = false;
                }
            }

            // console.log($scope.store);
        }
        $scope.get_goods = function() {
            /*goods = new Array();

            if($scope.dealer.goods == undefined) {
                GoDialog.toast("请选择活动商品");
            } else {
                for(var m = 0; m < $scope.dealer.goods.length; m++) {
                    if($scope.dealer.goods[m].groupbuy_rule == undefined)
                        $scope.dealer.goods[m].groupbuy_rule = new Array();
                    if($scope.dealer.goods[m].groupbuy_rule.length < 1) {
                        GoDialog.toast("请设定" + $scope.dealer.goods[m].goods_name + "的活动规则");
                        return;
                    }
                    var i = goods.length;
                    goods[i] = {
                        "goods_id": $scope.dealer.goods[m].goods_id,
                        "rule": $scope.dealer.goods[m].groupbuy_rule
                    };
                }
            }*/

            goods = new Object();
            goods = {
                "goods_id": $scope.dealer.goods_id,
                "margin_price": $scope.dealer.margin_price,
                "start_price": $scope.dealer.start_price,
                "add_price": $scope.dealer.add_price,
                "floor_price": $scope.dealer.floor_price,
            };

            // console.log(goods);
        }

        //
        $scope.submit = function() {
            $scope.get_region();
            $scope.get_goods();
            //$scope.get_region();
            if(region.length < 1) {
                GoDialog.toast("请添加代理商信息");
                return;
            }
            for(var i = 0; i < region.length; i++) {
                if(region[i].store == undefined) {
                    GoDialog.toast("请输入完整的地域信息");
                    return;
                } else if($scope.region[i].store.length < 1) {
                    GoDialog.toast("请输入完整的地域信息1");
                    return;
                }
            }
            Auction_agent.auction_edit("act_id", region, goods, function(data) {

                //console.log(data);
                if(data.success) {
                    GoDialog.toast("保存成功");
                } else {
                    GoDialog.toast("保存失败");
                }
                console.log(region);
                console.log(goods);
            });
            goods = new Object();
        }
        $scope.init_data();
        $scope.checked = function(id) {
            //console.log(id);
            for(var i = 0; i < $scope.store.length; i++) {
                if($scope.store[i].store_id == id) {
                    if($scope.store[i].checked == false) {
                        $scope.scope.is_all = false;

                        break;
                    } else {
                        if($scope.store.length == 1) {
                            $scope.scope.is_all = true;

                        }

                    }
                } else if($scope.store[i].checked == true) {
                    $scope.scope.is_all = true;

                } else {

                    $scope.scope.is_all = false;
                    break;
                }
            }
            //          console.log()
            //console.log($scope.dealer);
        }
        $scope.delete_store = function(region_name, index) {
            for(var i = 0; i < $scope.show_region.length; i++) {
                if($scope.show_region[i].region_name == region_name) {
                    $scope.region[i].store.splice(index, 1);
                    $scope.show_region[i].store.splice(index, 1);
                }
            }

        }
        $scope.delete_region = function(index) {
            $scope.show_region.splice(index, 1);
            region.splice(index, 1);
        }

    }

    controller.$inject = ['$scope', 'Auction_agent', '$window', '$timeout', '$ionicSlideBoxDelegate', 'GoPage', 'GoDialog', 'GoAddress'];

    return app.registerController("auction_agentctrl",controller);
});